package com.xywl.escort.view.bandcard;

import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.GetSmsCodeReq;
import com.xywl.escort.bean.GetUserBankCardTokenReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2017/10/25.
 */

public class BandCardSecondStepPresenter implements BandCardSecondStepContract.Presenter {
    private BandCardSecondStepContract.View mView;
    private ApiServer mApiServer;

    @Inject
    BandCardSecondStepPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        mView = null;
    }

    @Override
    public void takeView(BandCardSecondStepContract.View view) {
        this.mView = view;
    }

    @Override
    public void getUserBankCardToken(GetUserBankCardTokenReq req) {
        mApiServer.getUserBankCardToken(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.getUserBankCardTokenFailure("银行卡信息错误");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.getUserBankCardTokenSuccess(resp);
                            } else {
                                mView.getUserBankCardTokenFailure(resp.msg);
                            }
                        } else {
                            mView.getUserBankCardTokenFailure("银行卡信息错误");
                        }
                    }
                });
    }
}
